package com.mzj.saas.mservice.params.service;

import com.mzj.saas.commons.util.StringUtils;
import com.mzj.saas.mservice.params.entity.SystemParam;
import com.mzj.saas.mservice.params.repository.SystemParamRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class SystemParamService {
    private static final String MZJ_SYS_PARAMS = "MZJ_SYS_PARAMS";
    @Autowired
    private SystemParamRepository systemParamRepository;
    @Autowired
    @Qualifier("stringRedisTemplate")
    private StringRedisTemplate redisTemplate;
    public void synch() throws Exception {
        List<SystemParam> params = systemParamRepository.findAll();
        if (params == null || params.isEmpty()) {
            return;
        }
        HashOperations<String, Object, Object> hashOperation = redisTemplate.opsForHash();
        Map<String, String> paramMap = new HashMap<>();
        // 添加redis数据
        for (SystemParam entity : params) {
            paramMap.put(entity.getParamName(), entity.getParamValue());
        }
        hashOperation.putAll(MZJ_SYS_PARAMS, paramMap);
    }
    public void save(SystemParam vo) throws Exception {

        if (vo == null) {
            throw new Exception("数据格式不正确");
        }

        SystemParam entity = null;
        if (vo.getId() == null) {
            entity = new SystemParam();

            SystemParam findByParamName = systemParamRepository.findByParamName(vo.getParamName());
            if (findByParamName != null) {
                entity = findByParamName;// 添加的时候，已经存在的就不用管
            } else {
                if (StringUtils.isNotEmpty(vo.getParamName()) && !vo.getParamName().equals(entity.getParamName())) {
                    entity.setParamName(vo.getParamName());
                }
                if (StringUtils.isNotEmpty(vo.getParamValue()) && !vo.getParamValue().equals(entity.getParamValue())) {
                    entity.setParamValue(vo.getParamValue());
                }
                if (StringUtils.isNotEmpty(vo.getDescription())
                        && !vo.getDescription().equals(entity.getDescription())) {
                    entity.setDescription(vo.getDescription());
                }
                systemParamRepository.save(entity);
            }

        } else {// 修改
            Optional<SystemParam> existSystemParam = systemParamRepository.findById(vo.getId());
            if (existSystemParam!=null){
                entity = systemParamRepository.findById(vo.getId()).get();

            }
            if (StringUtils.isNotEmpty(vo.getParamName()) && !vo.getParamName().equals(entity.getParamName())) {
                entity.setParamName(vo.getParamName());
            }
            if (StringUtils.isNotEmpty(vo.getParamValue()) && !vo.getParamValue().equals(entity.getParamValue())) {
                entity.setParamValue(vo.getParamValue());
            }
            if (StringUtils.isNotEmpty(vo.getDescription()) && !vo.getDescription().equals(entity.getDescription())) {
                entity.setDescription(vo.getDescription());
            }
            systemParamRepository.save(entity);
        }

    }
}
